/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.service.processor;

import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.mail.domain.Mail;
import org.noear.solon.annotation.Component;
import org.simplejavamail.api.email.EmailPopulatingBuilder;
import org.simplejavamail.email.EmailBuilder;

/**
 * <b>(SendSimpleMailProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/26
 */
@SuppressWarnings("rawtypes")
@Component
public class SendSimpleMailProcessor extends AbstractMailServiceProcessor implements Processor {
    @SuppressWarnings("RedundantMethodOverride")
    @Override
    public Integer level() {
        return 50;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        Mail   mail          = (Mail) serviceContext.getDomain();
        String recipientName = StrUtil.split(mail.getTo().get(0), "@").get(0);
        EmailPopulatingBuilder simpleMailMessage = EmailBuilder.copying(email)
                .to(recipientName, mail.getTo().get(0))
                .withPlainText(mail.getContext());

        mailer.sendMail(simpleMailMessage.buildEmail());
    }
}